<template>
  <div class="execute">
    <Steps :active-step="0" />
    <c-permission-tabs v-model="activeType" :lazy="false">
      <el-tab-pane v-if="!isOnlyCheckArea" label="本次验收区域" name="checkArea">
        <CheckArea ref="checkArea" />
      </el-tab-pane>
      <CheckArea v-else ref="checkArea" style="display:none" />

      <!-- v-if="$route.query.hasStores==='yes'" -->
      <el-tab-pane label="非本次验收区域" name="noCheckArea">
        <NoCheckArea ref="noCheckArea" />
      </el-tab-pane>
    </c-permission-tabs>
    <div class="btn-container">
      <c-button type="primary" @click="saveHandle">保存</c-button>
      <c-button v-if="!isOnlyCheckArea" type="primary" @click="clickNextStep">下一步</c-button>
    </div>
  </div>
</template>
<script>
import CheckArea from './components/checkArea'
import NoCheckArea from './components/noCheckArea'
import Steps from './components/steps'
import { postBatchEdit } from '@/api/check'
export default {
  name: 'CheckHardExecute',
  components: {
    CheckArea,
    NoCheckArea,
    Steps
  },
  data() {
    return {
      isOnlyCheckArea: false,
      activeType: 'checkArea'
    }
  },
  created() {
    this.isOnlyCheckArea = this.$route.query.isOnlyCheckArea
    if (this.isOnlyCheckArea) this.activeType = 'noCheckArea'
  },
  methods: {
    submitData() {
      const data = []
      // for (let i = 0; this.$refs.checkArea.tableData.length > i; i++) {
      //   const d = this.$refs.checkArea.tableData[i]
      //   if (this.$toNumber(d.acceptArea) > this.$toNumber(d.actualOutpicArea)) {
      //     this.$message.warning(`${d.areaName}区域验收面积不能大于实际出图总面积！`)
      //     return Promise.reject('')
      //   }
      //   // actualOutpicArea
      //   // acceptArea
      //   if (this.$toNumber(d.acceptSeriesArea) > this.$toNumber(d.seriesOutpicArea)) {
      //     this.$message.warning(`第${d.keyIndex + 1}行系列验收面积不能大于系列出图面积！`)
      //     return Promise.reject('')
      //   }
      // }
      let t1 = []
      if (this.$refs.checkArea) {
        let t
        if (this.isOnlyCheckArea) t = this.$refs.checkArea.getTableData()
        else {
          t = this.$refs.checkArea.validate(false)
          if (!t) {
            return Promise.reject('')
          }
        }
        t1 = t
      }
      let t2 = []
      if (this.$refs.noCheckArea) {
        const t = this.$refs.noCheckArea.validate(false)
        if (!t) {
          return Promise.reject('')
        }
        t2 = t
      }
      if (t1.length === 0 && t2.length === 0) {
        this.$message.warning('请添加区域！')
        return Promise.reject('')
      }
      data.push(...t1.map(e => {
        e = { ...e }
        e.types = 1
        e.acceptId = this.$route.query.id
        return e
      }))
      if (this.$refs.noCheckArea) {
        data.push(...t2.map(e => {
          e = { ...e }
          e.types = 0
          e.acceptId = this.$route.query.id
          return e
        }))
      }
      return postBatchEdit(data).then(res => {
        this.$message.success('保存成功！')
      })
    },
    saveHandle() {
      return this.submitData().then(() => {
        this.$store.dispatch('views/goFirst')
      })
    },
    clickNextStep() {
      return this.submitData().then(() => {
        this.$router.push({ name: 'CheckHardExecuteProblem', query: this.$route.query })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.execute{
  padding-top: 20px;
}
</style>
